package com.nCovInfo.config;

import cn.hutool.log.StaticLog;
import com.alibaba.fastjson.JSONArray;
import com.nCovInfo.pojo.User;
import com.nCovInfo.util.CommonUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class GlobalUserMap {

	/**
	 * 全局用户信息
	 */
	private static ConcurrentHashMap<Integer,User> userMap = new ConcurrentHashMap();

	/**
	 * 获取全量user信息
	 * @return
	 */
	public static Map<Integer,User> getUserMap(){
		return userMap;
	}

	@PostConstruct
	public void initUserMap(){
		JSONArray userArray = CommonUtils.getUserList(true);
		userArray.stream().forEach(user -> userMap.put(((User)user).getId(),(User)user));
	}

	@PreDestroy
	public void destory(){
		JSONArray array = new JSONArray();
		userMap.values().stream().forEach(user -> {
				user.setIdCardNum(CommonUtils.encryptAES(user.getIdCardNum()));
				user.setTelNum(CommonUtils.encryptAES(user.getTelNum()));
				array.add(user);
		});
		try {
			StaticLog.info("destory save----->");
			CommonUtils.writeFile(array);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
